package cn.mujiankeji.mbrowser.功能.播放框.组件

import android.content.ComponentName
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.session.MediaController
import androidx.media3.session.SessionToken
import androidx.media3.ui.AspectRatioFrameLayout
import androidx.media3.ui.PlayerView
import cn.mujiankeji.mbrowser.功能.播放框.服务.PlaybackService
import com.google.common.util.concurrent.ListenableFuture
import com.google.common.util.concurrent.MoreExecutors

/**
 * 带有授权令牌的HLS播放器组件
 * 使用MediaSessionService在后台播放HLS流
 * 
 * @param url HLS流URL
 * @param token 授权令牌
 * @param modifier Modifier修饰符
 */
@UnstableApi
@Composable
fun HlsPlayerWithToken(
    url: String,
    token: String,
    modifier: Modifier = Modifier
) {
    val context = LocalContext.current
    
    // 创建到MediaSessionService的连接
    val sessionToken = remember {
        SessionToken(context, ComponentName(context, PlaybackService::class.java))
    }
    
    var controllerFuture: ListenableFuture<MediaController>? = remember { null }
    
    DisposableEffect(url, token) {
        // 连接到MediaSessionService
        val future = MediaController.Builder(context, sessionToken).buildAsync()
        controllerFuture = future
        
        // 当控制器准备好时，播放带有令牌的HLS流
        future.addListener({
            val controller = future.get()
            
            // 设置媒体项并播放
            val mediaItem = PlaybackService.createMediaItemWithToken(url, token)
            controller.setMediaItem(mediaItem)
            controller.prepare()
            controller.play()
            
        }, MoreExecutors.directExecutor())
        
        onDispose {
            // 释放资源
            controllerFuture?.let { MediaController.releaseFuture(it) }
        }
    }
    
    // 播放器视图
    Box(modifier = modifier.fillMaxWidth()) {
        AndroidView(
            factory = {
                PlayerView(context).apply {
                    useController = true
                    resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
                    
                    controllerFuture?.addListener({
                        player = controllerFuture?.get()
                    }, MoreExecutors.directExecutor())
                }
            },
            modifier = Modifier
                .fillMaxWidth()
                .aspectRatio(16f / 9f)
        )
    }
}

/**
 * 使用示例:
 * 
 * HlsPlayerWithToken(
 *     url = "https://example.com/stream.m3u8",
 *     token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
 *     modifier = Modifier.fillMaxWidth()
 * )
 */ 